package com.ruoyi.record.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.domain.RecordSalary;
import com.ruoyi.record.service.IRecordSalaryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工资信息Controller
 * 
 * @author ruoyi
 * @date 2023-02-12
 */
@Controller
@RequestMapping("/record/salary")
public class RecordSalaryController extends BaseController
{
    private String prefix = "record/salary";

    @Autowired
    private IRecordSalaryService recordSalaryService;

    @RequiresPermissions("record:salary:view")
    @GetMapping()
    public String salary()
    {
        return prefix + "/salary";
    }

    /**
     * 查询工资信息列表
     */
    @RequiresPermissions("record:salary:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordSalary recordSalary)
    {
        startPage();
        List<RecordSalary> list = recordSalaryService.selectRecordSalaryList(recordSalary);
        return getDataTable(list);
    }

    /**
     * 新增工资信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("salaryMonth", DateUtils.getMonth());
        return prefix + "/add";
    }

    /**
     * 新增保存工资信息
     */
    @RequiresPermissions("record:salary:add")
    @Log(title = "工资信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordSalary recordSalary)
    {
        if(StringUtils.isEmpty(recordSalary.getSalaryMonth())) {
            return error("新增失败，请选择工资月份");
        }
        if(recordSalaryService.checkRecordSalary(recordSalary)) {
            recordSalary.setCreateBy(ShiroUtils.getLoginName());
            return toAjax(recordSalaryService.insertRecordSalary(recordSalary));
        }else {
            return error("当前月份已存在考勤记录");
        }
    }

    /**
     * 修改工资信息
     */
    @GetMapping("/edit/{salaryId}")
    public String edit(@PathVariable("salaryId") Long salaryId, ModelMap mmap)
    {
        RecordSalary recordSalary = recordSalaryService.selectRecordSalaryBySalaryId(salaryId);
        mmap.put("recordSalary", recordSalary);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资信息
     */
    @RequiresPermissions("record:salary:edit")
    @Log(title = "工资信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordSalary recordSalary)
    {
        if(StringUtils.isEmpty(recordSalary.getSalaryMonth())) {
            return error("修改失败，请选择工资月份");
        }
        if(recordSalaryService.checkRecordSalary(recordSalary)) {
            recordSalary.setUpdateBy(ShiroUtils.getLoginName());
            return toAjax(recordSalaryService.updateRecordSalary(recordSalary));
        }else {
            return error("当前月份已存在考勤记录");
        }
    }

    /**
     * 删除工资信息
     */
    @RequiresPermissions("record:salary:remove")
    @Log(title = "工资信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordSalaryService.deleteRecordSalaryBySalaryIds(ids));
    }
}
